package kr.co.insoft.root;

import kr.co.insoft.auth.model.members.JoinFormModel;
import kr.co.insoft.auth.model.members.TermsFormModel;
import kr.co.insoft.auth.model.members.validator.JoinFormValidator;
import kr.co.insoft.auth.model.members.validator.TermsFormValidator;
import kr.co.insoft.auth.service.members.JoinService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Handles requests for the application home page.
 */
@Controller
public class MainController {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Model model) {
		return "tiles/single/login";
	}

	@RequestMapping(value = "login")
	public String login(Model model) {
		return "tiles/single/login";
	}

	@RequestMapping(value = "loginFailed")
	public String loginFailed(Model model) {
		return "tiles/single/loginFailed";
	}

	@RequestMapping(value = "joinStep1", method = RequestMethod.GET)
	public String joinStep1(Model model) {
		return "tiles/single/joinStep1";
	}

	/**
	 * 약관 페이지
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "ut{userType:n|c}/joinStep2", method = RequestMethod.GET)
	public String joinStep2(Model model,
			@PathVariable("userType") String userType) {
		model.addAttribute(new TermsFormModel());
		return "tiles/single/joinStep2";
	}

	/**
	 * 약관 동의 확인 처리 후, 가입 페이지로
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "ut{userType:n|c}/joinStep2", method = RequestMethod.POST)
	public String joinStep1Process(
			Model model,
			@PathVariable("userType") String userType,
			@Validated @ModelAttribute("termsFormModel") TermsFormModel termsFormModel,
			final BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return "tiles/single/joinStep2";
		}
		return "redirect:/ut" + userType + "/join";
	}

	@Autowired
	TermsFormValidator termsFormValidator;

	@InitBinder("termsFormModel")
	protected void TermsFormModelBinder(final WebDataBinder binder) {
		binder.setValidator(termsFormValidator);
	}

	/**
	 * 가입 페이지
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "ut{userType:n|c}/join", method = RequestMethod.GET)
	public String joinStepLast(Model model,
			@PathVariable("userType") String userType) {
		JoinFormModel joinForm = new JoinFormModel();
		joinForm.setCompany(userType.equals("c"));
		model.addAttribute("joinFormModel", joinForm);
		return "tiles/single/join";
	}

	@Autowired
	JoinService joinService;

	/**
	 * 가입 처리
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "join", method = RequestMethod.POST)
	public String join(
			Model model,
			@Validated @ModelAttribute("joinFormModel") JoinFormModel joinFormModel,
			final BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return "tiles/single/join";
		}
		joinService.registMember(joinFormModel);
		return "redirect:/";
	}

	@Autowired
	JoinFormValidator joinFormValidator;

	@InitBinder("joinFormModel")
	protected void JoinFormModelBinder(final WebDataBinder binder) {
		binder.setValidator(joinFormValidator);
	}

	@RequestMapping(value = "account")
	public String accountRevitalize(
			@RequestParam("confirmKey") String confirmKey) throws Exception {
		joinService.accountRevitalize(confirmKey);
		return "tiles/single/accountRevitalize";
	}
}
